package com.example.PDFdemo.json;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.itextpdf.text.DocumentException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;


public class Test {

    //能够读取到json文件
    public static String downloadJson (String url) {
        StringBuilder json = new StringBuilder();
        try {
            URL urlObject = new URL(url);
            URLConnection uc = urlObject.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(),"UTF-8"));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }

    public static void main(String[] args) throws DocumentException, IOException {
            String oldFilePath = "C:\\Users\\admin\\Desktop\\test1.pdf";
            String newFilePath = "C:\\Users\\admin\\Desktop\\test2.pdf";
            String jsonUrl = "https://testyinji.gaoshan.co/oss/gs2021/20221114/84317854633b5a07cf09dd14c60f21179ca544de6e7759abee2ada800e488227.json";
        //获取json字符串
        String jsonStr = downloadJson(jsonUrl);
        //解析json字符串，生成Java对象
        JSONArray contents = JSONUtil.parseArray(jsonStr);
        List<PDFModels> jsonList = contents.toList(PDFModels.class);
        List<FillInfo> fillInfoList = new ArrayList<>();
        Base64Decoder decoder = new Base64Decoder();
        for (PDFModels json : jsonList) {
            String src = json.getBase64Img();
            String content = src.substring(src.lastIndexOf(",") + 1);
            byte[] contentSrc = decoder.decode(content);
            fillInfoList.add(FillInfo.builder()
                    .sh(json.getH())
                    .sw(json.getW())
                    .sx(json.getX())
                    .sy(json.getY())
                    .pageNo(json.getP() + 1)
                    .content(contentSrc)
                    .build()
            );
        }
        for (FillInfo info : fillInfoList) {
            System.out.println(info);
        }
        PDFUtil.fillPdf(oldFilePath, newFilePath, fillInfoList);


/*
        String oldFilePath = "C:\\Users\\admin\\Desktop\\test1.pdf";
        String newFilePath = "C:\\Users\\admin\\Desktop\\test2.pdf";
        Map<String, Integer> map = new HashMap<>();
        //正常
        map.put("normal", 0);
        //加粗
        map.put("bold", 1);
        //斜体
        map.put("italic", 1);
        //水平对齐方式
        map.put("horizontal-left", 0);
        map.put("horizontal-center", 1);
        map.put("horizontal-right", 2);
        //垂直对齐方式
        map.put("vertical-flex-start", 4);
        map.put("vertical-center", 5);
        map.put("vertical-flex-end", 6);
        JSONArray contents = JSONUtil.parseArray("[{\"p\":0,\"w\":0.23780584473920627,\"h\":0.040398750631230475,\"x\":0,\"y\":0.9596012493687694,\"order\":0,\"type\":\"text\",\"content\":\"天上白玉京,十二楼五城。\",\"style\":{\"attName\":\"单行文本1\",\"fontWeight\":\"normal\",\"color\":\"#000000\",\"fontStyle\":\"normal\",\"textAlign\":\"left\",\"fontSize\":\"12\",\"fontFamily\":\"宋体\"}},{\"p\":0,\"w\":0.23780584473920627,\"h\":0.040398750631230475,\"x\":0.76125,\"y\":0.9578316531692446,\"order\":1,\"type\":\"text\",\"content\":\"仙人抚我顶,结发授长生。\",\"style\":{\"attName\":\"单行文本2\",\"fontWeight\":\"normal\",\"color\":\"#000000\",\"fontStyle\":\"normal\",\"textAlign\":\"left\",\"fontSize\":\"12\",\"fontFamily\":\"宋体\"}},{\"p\":0,\"w\":0.23780584473920627,\"h\":0.12119625189369143,\"x\":0,\"y\":0.7903239381300616,\"order\":0,\"type\":\"multText\",\"content\":\"误逐世间乐，颇穷理乱情。九十六圣君，浮云挂空名。天地赌一掷，未能忘战争。试涉霸王略，将期轩冕荣。时命乃大谬，弃之海上行。\",\"style\":{\"attName\":\"多行文本1\",\"fontWeight\":\"normal\",\"color\":\"#000000\",\"fontStyle\":\"normal\",\"textAlign\":\"left\",\"alignItems\":\"flex-start\",\"justifyContent\":\"flex-start\",\"fontSize\":\"12\",\"fontFamily\":\"宋体\"}},{\"p\":0,\"w\":0.23780584473920627,\"h\":0.12119625189369143,\"x\":0.76125,\"y\":0.8009415153272113,\"order\":1,\"type\":\"multText\",\"content\":\"学\",\"style\":{\"attName\":\"多行文本2\",\"fontWeight\":\"normal\",\"color\":\"#000000\",\"fontStyle\":\"normal\",\"textAlign\":\"center\",\"alignItems\":\"center\",\"justifyContent\":\"flex-start\",\"fontSize\":\"12\",\"fontFamily\":\"宋体\"}},{\"p\":0,\"w\":0.23780584473920627,\"h\":0.040398750631230475,\"x\":0,\"y\":0.721590560532665,\"order\":0,\"type\":\"radio\",\"selected\":false,\"style\":{\"attName\":\"选项1\",\"radioName\":\"选项1\",\"radioType\":0,\"groupName\":\"选项\",\"fontWeight\":\"normal\",\"color\":\"#000000\",\"fontStyle\":\"normal\",\"fontSize\":\"12\",\"fontFamily\":\"宋体\"}},{\"p\":0,\"w\":0.23780584473920627,\"h\":0.040398750631230475,\"x\":0.76125,\"y\":0.7189361662333776,\"order\":1,\"type\":\"radio\",\"selected\":false,\"style\":{\"attName\":\"选项2\",\"radioName\":\"选项2\",\"radioType\":0,\"groupName\":\"选项\",\"fontWeight\":\"normal\",\"color\":\"#000000\",\"fontStyle\":\"normal\",\"fontSize\":\"12\",\"fontFamily\":\"宋体\"}},{\"p\":0,\"w\":0.23780584473920627,\"h\":0.16832812763012697,\"x\":0,\"y\":0.5069509697570465,\"order\":0,\"type\":\"image\",\"imgUrl\":\"https://img1.baidu.com/it/u=617142265,3752425998&fm=253&fmt=auto&app=138&f=JPEG?w=450&h=350\",\"style\":{\"attName\":\"图片1\"}},{\"p\":0,\"w\":0.23780584473920627,\"h\":0.16832812763012697,\"x\":0.76125,\"y\":0.49013980586155953,\"order\":1,\"type\":\"image\",\"imgUrl\":\"https://img1.baidu.com/it/u=617142265,3752425998&fm=253&fmt=auto&app=138&f=JPEG?w=450&h=350\",\"style\":{\"attName\":\"图片2\"}}]");
        List<FillInfo> fillInfoList = new ArrayList<>();
        List<PDFModels> models = contents.toList(PDFModels.class);
        for (PDFModels model : models) {
            fillInfoList.add(FillInfo.builder()
                            .sh(model.getH())
                            .sw(model.getW())
                            .sx(model.getX())
                            .sy(model.getY())
                            .content(model.getContent())
                            .pageNo(model.getP() + 1)
                            .fontName(model.getStyle().getFontFamily())
                            .imgUrl(model.getImgUrl())
                            .fontSize(model.getStyle().getFontSize())
                            .fontBold(map.get(model.getStyle().getFontWeight()))
                            .fontIncline(map.get(model.getStyle().getFontStyle()))
                            .horizontal(map.get("horizontal-" + model.getStyle().getTextAlign()))
                            .vertical(map.get("vertical-" + model.getStyle().getAlignItems()))
                            .color(model.getStyle().getColor())
                            .type(model.getType())
                    .build());
        }
        for (FillInfo info : fillInfoList) {
            System.out.println(info);
            System.out.println();
        }
        PDFUtil.addText(oldFilePath, newFilePath, fillInfoList);
*/
    }
}
